package com.srit.map.business.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.srit.core.ibatis.dao.SqlMapDao;
import com.srit.map.business.bean.Role;
import com.srit.map.business.bean.RoleAndMenu;
import com.srit.map.business.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService {
	@Autowired(required = true)
	private SqlMapDao sqlMapDao;
	
	
	@Override
	public Role getRoleByName(String name) {
		Role role=(Role)sqlMapDao.queryForObject("Role.getRoleByName", name);
		return role;
	}

	@Override
	public Role getRoleByUserId(int UserId) {
		Role role=(Role)sqlMapDao.getById("Role.get_role_by_userid", UserId);
		return role;
	}

	@Override
	public void saveMenuToRole(String menuIdStr, Role role) {
		sqlMapDao.save("Role.save", role);
		String [] str=menuIdStr.split(",");
		int [] menuId=new int[str.length];
        for(int i=0;i<str.length;i++){
        	menuId[i]=Integer.parseInt(str[i]);
        }
		for(int i=0;i<str.length;i++){
			role=this.getRoleByName(role.getName());
			RoleAndMenu roleMenu=new RoleAndMenu();
			roleMenu.setRoleId(role.getId());
			roleMenu.setMenuId(menuId[i]);
			sqlMapDao.save("Role.saveWithMenu", roleMenu);
		}
	}

	@Override
	public void updateMenuToRole(String menuIdStr, Role role) {
		// TODO Auto-generated method stub
		sqlMapDao.delete("Role.deleteMenuOfRole", role.getId());
		String [] str=menuIdStr.split(",");
		int [] menuId=new int[str.length];
        for(int i=0;i<str.length;i++){
        	menuId[i]=Integer.parseInt(str[i]);
        }
		for(int i=0;i<str.length;i++){
			RoleAndMenu roleMenu=new RoleAndMenu();
			roleMenu.setRoleId(role.getId());
			roleMenu.setMenuId(menuId[i]);
			sqlMapDao.save("Role.saveWithMenu", roleMenu);
		}
		
	}

	@Override
	public List getRoleList() {
		List list = sqlMapDao.queryForList("", null);
		return null;
	}
	
	
	

}
